<?php


namespace app\model;

use fast\Http;
use think\facade\Log;
use think\Model;

class Albums extends Model {

    protected $service;
    public function __construct(array $data = []){
        $service_type=config('capture.platform');
        eval('$this->service= new app\service\impl\Albums'.$service_type.'ServiceImpl();');
        parent::__construct($data);
    }

    /**
     * 同步底库列表
     * @return void
     */
    public function sys_lists($alubum_id='',$follow=0){
        $lists=$this->service->sys_lists();
        $new_ids=[];
        $old_ids=[];
        $olds=$this->where('del_flag','=',0)->column('id,album_id,album_type,name,level,note,auth_type','album_id');
        if($olds){
            $old_ids=array_keys($olds);
        }
        $new_album_id=0;
        foreach ($lists as $list) {
            $list['del_flag']=0;
            $list['parent_id']=0;
            if($list['album_id']==$alubum_id){
                $list['is_neijiang']=1;
                if($follow==1){
                    $list['parent_id']=-1;
                }
            }
            $new_ids[]=$list['album_id'];
            if(isset($olds[$list['album_id']])){
                $this->where('album_id',$list['album_id'])->save($list);
            }else{
                if($list['note']==$alubum_id){
                    $list['parent_id']=$new_album_id;
                }
                $id=$this->insertGetId($list);
                if($list['album_id']==$alubum_id){
                    $new_album_id=$id;
                }
                Log::info('新目录：'.json_encode($list).'___'.$id);
            }
        }
        $dels=array_diff($old_ids,$new_ids);
        if($dels){
            $this->where('album_id','in',$dels)->save(['del_flag'=>1]);
        }
    }

    public function create_album($name,$note,$level,$follow){
        $album=$this->service->create_album(['name'=>$name,'note'=>$note,'level'=>$level]);
        $album=json_decode($album,true);
        $album_id='';
        if(isset($album['data']['album_id'])){
            $album_id=$album['data']['album_id'];
            if($follow==1){
                $this->service->create_album(['name'=>$name.'_监护人','note'=>$album_id,'level'=>$level]);
            }
        }
        Log::info("album_id:".$album_id);
        $this->sys_lists($album_id,$follow);
        if(!$album){
            return '新增失败';
        }elseif(!$album_id){
            return $album['msg'];
        }else{
            return '';
        }
    }

    public function edit_album($id,$name,$note,$level){
        $info=$this->find($id);
        if($info){
            $this->service->edit_album(['album_id'=>$info['album_id'],'name'=>$name,'note'=>$note,'level'=>$level]);
            $this->sys_lists();
        }else{
            return false;
        }

    }

    public function del_album($id){
        $info=$this->find($id);
        if($info){
            $this->service->del_album(['album_id'=>$info['album_id']]);
            $this->sys_lists();
        }else{
            return false;
        }

    }
}